#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const int N = 5e2 + 10, INF = 0x3f3f3f3f;

int n;
int a[N];
int f[N][N];

void solve()
{
    cin >> n;
    for (int i = 1;i <= n;i++) cin >> a[i];

    memset(f, INF, sizeof f);
    for (int i = 1;i <= n;i++) f[i][i] = 1;
    for (int i = 1;i < n;i++) f[i][i + 1] = (a[i] != a[i + 1]) + 1;

    for (int len = 3;len <= n;len++)
        for (int l = 1;l + len - 1 <= n;l++) {
            int r = l + len - 1;
            if (a[l] == a[r]) f[l][r] = f[l + 1][r - 1];
            for (int k = l;k < r;k++)
                f[l][r] = min(f[l][r], f[l][k] + f[k + 1][r]);

        }
    cout << f[1][n];
}

signed main()
{
    //ios::sync_with_stdio(0);
    //cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    int T = 1;
    //cin>>T;
    for (int i = 1;i <= T;i++) {
        solve();
    }
}